package com.joinway.mobile

import grails.converters.JSON
import groovy.util.logging.Slf4j

import org.restapidoc.annotation.RestApi
import org.restapidoc.annotation.RestApiBodyObject
import org.restapidoc.annotation.RestApiMethod
import org.restapidoc.annotation.RestApiParam
import org.restapidoc.annotation.RestApiParams
import org.restapidoc.annotation.RestApiResponseObject
import org.restapidoc.pojo.RestApiParamType
import org.restapidoc.pojo.RestApiVerb
import org.springframework.http.MediaType

import com.joinway.bean.View;
import com.joinway.mobile.bean.view.LoginView
import com.joinway.mobile.bean.view.VersionView

@RestApi(name = "手机后台接口", description = "手机后台通用接口")
@Slf4j
class PortalController {

	MobileService mobileService;
	
	@RestApiMethod(description="用户注册", verb=RestApiVerb.POST, consumes=MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	@RestApiParams(params=[
		@RestApiParam(name="loginName", type="string", paramType = RestApiParamType.QUERY, description = "用户名,6-16位")
		, @RestApiParam(name="password", type="string", paramType = RestApiParamType.QUERY, description = "密码,6-16位")
		, @RestApiParam(name="cellPhone", type="string", paramType = RestApiParamType.QUERY, description = "手机号")
		, @RestApiParam(name="cellPhoneType", type="string", paramType = RestApiParamType.QUERY, description = "手机类型")
	])
	@RestApiBodyObject(name="none")
	@RestApiResponseObject(objectIdentifier="[LoginView]")
    LoginView register() { 
		log.info "loginName is ${params.loginName}"
		
		mobileService.register()
		
		render new LoginView() as JSON
	}
	
	@RestApiMethod(description="用户登录", verb=RestApiVerb.POST, consumes=MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	@RestApiParams(params=[
		@RestApiParam(name="loginName", type="string", paramType = RestApiParamType.QUERY, description = "用户名,6-16位")
		, @RestApiParam(name="password", type="string", paramType = RestApiParamType.QUERY, description = "密码,6-16位")
		, @RestApiParam(name="cellPhoneType", type="string", paramType = RestApiParamType.QUERY, description = "手机类型")
	])
	@RestApiBodyObject(name="none")
	@RestApiResponseObject(objectIdentifier="[LoginView]")
	LoginView login(){
		render new LoginView() as JSON
	}
	
	@RestApiMethod(description="查看最新版本", verb=RestApiVerb.GET, consumes=MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	@RestApiBodyObject(name="none")
	@RestApiResponseObject(objectIdentifier="[VersionView]")
	VersionView version(){
		render new VersionView() as JSON
	}
	
	@RestApiMethod(description="修改密码", verb=RestApiVerb.POST, consumes=MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	@RestApiParams(params=[
		@RestApiParam(name="loginName", type="string", paramType = RestApiParamType.QUERY, description = "用户名,6-16位")
		, @RestApiParam(name="oldPassword", type="string", paramType = RestApiParamType.QUERY, description = "旧密码,6-16位")
		, @RestApiParam(name="newPassword", type="string", paramType = RestApiParamType.QUERY, description = "新密码,6-16位")
	])
	@RestApiBodyObject(name="none")
	@RestApiResponseObject(objectIdentifier="[View]")
	View password(){
		render new View() as JSON
	}
}
